package com.fernfx.xingtan.my.presenter;

import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.MyPocketMoneyContract;
import com.fernfx.xingtan.my.entity.MyPocketMoneyEntity;
import com.fernfx.xingtan.my.model.MyPocketMoneyModel;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/20.
 */

public class MyPocketMoneyPresenter<P extends MyPocketMoneyContract.View> extends BaseIRequestCallback implements MyPocketMoneyContract.Presenter{
    private P P;
    private MyPocketMoneyContract.Model model;

    @Override
    public void init(BaseView view) {
        P = (P)view;
        this.model = new MyPocketMoneyModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        P.getActivity().showLoading();
        model.request(requestArgsMap,this);
    }

    @Override
    public void detachView() {
        if (model != null) {
            model = null;
        }
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        P.getActivity().hideLoading();
        String message = result.getMessage();
        MyPocketMoneyEntity myPocketMoneyEntity = FastJsonUtil.fromBean(message, MyPocketMoneyEntity.class);
        if (OtherUtil.checkRequestStatus(myPocketMoneyEntity)) {
            P.displayMoney(myPocketMoneyEntity);
        }else {
            ToastUtil.showCentertoast(myPocketMoneyEntity.getMsg());
        }
    }
}
